package com.coscon.test;



import cn.hutool.core.io.FileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 查找重名的文件夹 {

    final static String 根路径 = "/Volumes/4TB-DISK-MUSIC/音乐/无损音乐备份";

    public void 执行查找(){
        Map<String,List<String>> mapData = new HashMap<>();
        //递归遍历所有目录
        List<File> files = (List<File>) FileUtils.listFilesAndDirs(new File(根路径), TrueFileFilter.INSTANCE, DirectoryFileFilter.DIRECTORY);
        for(File f : files){
            boolean isDir = FileUtils.isDirectory(f);
            String name =f.getName();
            if(!isDir){
                continue;
            }
            String dirPath = f.getPath();
            //先判断是否已经存在
            boolean isHave = mapData.containsKey(name);
            if(isHave){
                List<String> listPath = mapData.get(name);
                listPath.add(dirPath);
                mapData.put(name,listPath);
            }else{
                List<String> listPath = new ArrayList<>();
                listPath.add(dirPath);
                mapData.put(name,listPath);
            }

        }

        //遍历map
        for (Map.Entry<String,List<String>> entry : mapData.entrySet()) {
            String dirName = entry.getKey();
            List<String> listDirPath = entry.getValue();
            if(listDirPath.size()>1){
                System.out.println("有重名的文件夹：" + dirName  );
                for(String dir: listDirPath){
                    System.out.println("\t路径是："+ dir );
                }
            }
        }


    }


    public static void main(String[] args) {
        查找重名的文件夹 a = new 查找重名的文件夹();
        a.执行查找();

    }
}
